package threadLearn;

import java.util.Arrays;
import java.util.Random;

public class demon4 {
    public static void main1(String[] args) {
        int[] arr = new int[100000000];
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            int rundomNum = random.nextInt(1, 100) + 1;
            arr[i] = rundomNum;
        }
//        for (int i = 0; i < 100; i++) {
//            System.out.println(arr[i]);
//        }
        int sum2 = 0;
        //计算偶数
        Thread thread1 = new Thread(() -> {
            int sum1 = 0;
            for (int i = 0; i < 100; i++) {
                if (arr[i] % 2 == 0) {
                    sum1 += arr[i];
                }
            }
        });
        thread1.start();


        //计算奇数
        Thread thread2 = new Thread(() -> {


        });


    }

    public static void main2(String[] args) {
        int[] array = generateRandomArray(1000000); // 生成长度为1000000的随机数组

        long startTime = System.currentTimeMillis(); // 记录开始时间

        // 创建两个线程分别计算偶数下标元素的和和奇数下标元素的和
        Thread thread1 = new Thread(() -> {
            long evenSum = 0;
            for (int i = 0; i < array.length; i += 2) {
                evenSum += array[i];
            }
            System.out.println("偶数下标元素的和：" + evenSum);
        });

        Thread thread2 = new Thread(() -> {
            long oddSum = 0;
            for (int i = 1; i < array.length; i += 2) {
                oddSum += array[i];
            }
            System.out.println("奇数下标元素的和：" + oddSum);
        });

        thread1.start();
        thread2.start();

        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 将奇数和偶数和相加
        long totalSum = 0;

        System.out.println("奇数和偶数和相加的结果：" + totalSum);

        long endTime = System.currentTimeMillis(); // 记录结束时间
        System.out.println("计算总时间：" + (endTime - startTime) + "毫秒");
    }

    // 生成指定长度的随机数组
    private static int[] generateRandomArray(int length) {
        int[] array = new int[length];
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            array[i] = random.nextInt(100) + 1; // 生成1-100之间的随机整数
        }
        return array;
    }

    public static void main3(String[] args) {
        Thread thread0=new Thread(()->{

        });
        Thread thread1=new Thread(()->{

        });
        Thread thread2=new Thread(()->{

        });Thread thread3=new Thread(()->{

        });Thread thread4=new Thread(()->{

        });Thread thread5=new Thread(()->{

        });Thread thread6=new Thread(()->{

        });Thread thread7=new Thread(()->{

        });Thread thread8=new Thread(()->{

        });Thread thread9=new Thread(()->{

        });Thread thread10=new Thread(()->{

        });Thread thread11=new Thread(()->{

        });Thread thread12=new Thread(()->{

        });
    }
}

